/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package runattime;


import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.IOException;
import java.util.Calendar;

/**
 *
 * @author Kubik
 */
public class Casovac {
    private java.util.Calendar calendar = new java.util.GregorianCalendar();//TODO http://www.devdaily.com/java/java-date-add-calendar-add-example
    private java.util.Timer timer = new java.util.Timer();
    private Shutdown ukol = new Shutdown();
    private static java.lang.Process p;
    
    public Casovac() throws java.io.IOException{
    }
    
    public Casovac(String cesta){
        this.runExe(cesta);
    }
    
    public void runExe(String cesta){
        try{
            JFileChooser chooser = new JFileChooser();
            chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            if(chooser.showOpenDialog(null) != JFileChooser.APPROVE_OPTION)
                throw new java.io.IOException("Musíte zadat cestu pro soubory!");
            
            boolean pause = true;
            if(cesta.isEmpty())
                throw new java.io.IOException("Musíte zadat cestu pro soubory!");
            p = java.lang.Runtime.getRuntime().exec(this.getFolder()+" \""+cesta+"\"");
            timer.schedule(ukol, calendar.getTime());
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Nepodařilo se spustit program! Důvod: "+e.getMessage());
        }
    }
    
    public void runExe(){
        try{
            JFileChooser chooser = new JFileChooser();
            chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            if(chooser.showOpenDialog(null) != JFileChooser.APPROVE_OPTION)
                throw new java.io.IOException("Musíte zadat cestu pro soubory!");
            
            boolean pause = true;
            String cesta = chooser.getSelectedFile().getAbsolutePath();
            if(cesta.isEmpty())
                throw new java.io.IOException("Musíte zadat cestu pro soubory!");
            p = java.lang.Runtime.getRuntime().exec(this.getFolder()+" \""+cesta+"\"");
            timer.schedule(ukol, calendar.getTime());
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Nepodařilo se spustit program! Důvod: "+e.getMessage());
        }
    }
    
    public static void quitProcess() throws java.lang.Exception{
        if(p==null)
            throw new java.lang.Exception("Proces není spuštěný! ");
        else
            p.destroy();
    }
    
    private String getFolder() throws java.io.IOException{
        String output = "";
        try{
            java.lang.Process q = java.lang.Runtime.getRuntime().exec("reg query HKEY_CLASSES_ROOT\\Applications\\KMPlayer.exe\\shell\\open\\command");
            StreamReader reader = new StreamReader(q.getInputStream());
            reader.start();
            q.waitFor();
            reader.join();
            output = reader.getResult();
            if(!output.contains("\t")){
                throw new java.io.IOException("Chyba při zjišťování cesty! Output: "+output.toString());
            }
            String[] parsed = output.split("\t");
            output = parsed[parsed.length-1];
            String[] tmp = output.split("\"");
            output = tmp[1];
        }catch(java.io.IOException e){
            JOptionPane.showMessageDialog(null, "Nastala chyba při zjišťování cesty! "+e.getMessage());
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Nastala nečekaná chyba při zjišťování cesty! "+e.getMessage());
        }
        return output;
    }
    
    static class StreamReader extends Thread {
        private InputStream is;
        private StringWriter sw= new StringWriter();

        public StreamReader(InputStream is) {
            this.is = is;
        }
        
        @Override
        public void run() {
            try {
                int c;
                while ((c = is.read()) != -1)
                    sw.write(c);
            }
            catch (IOException e) { 
        }
        }

        public String getResult() {
            return sw.toString();
        }
    }
    
    public void setShutdown(int rok, int mesic, int den, int hodina, int minuta){
        try{
            calendar.set(rok, mesic, den, hodina, minuta, 00);
            java.util.Date test = calendar.getTime();
            boolean pause = true;
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Nastala chyba při nastavování data! "+e.getMessage());
        }
    }
}
